package sistem.management.appstart;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import sistem.manangement.model.UserType;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DoAtStart implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key adminKey = KeyFactory.createKey("user", "admin@admin.com");
		Entity userEntity;
		try {
			userEntity = datastore.get(adminKey);
			System.out.println("Exista admin cu adresa: " + userEntity.getProperty("email"));
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
			System.out.println("Nu exista admin, va fi creat");
			userEntity = new Entity(adminKey);
			userEntity.setProperty("email", "admin@admin.com");
			userEntity.setProperty("nume", "Mister");
			userEntity.setProperty("prenume", "Admin");
			userEntity.setProperty("usertype", UserType.ADMINISRTATOR.toString());
			datastore.put(userEntity);
		}

	}

}
